package com.wzcl.app.config.oauth.service;

import com.wzcl.app.config.oauth.model.UserDetailsImpl;
import com.wzcl.app.dao.model.AppUser;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Log4j2
public class MyUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private com.wzcl.app.service.UserService UserService;

    @Override
    public UserDetails loadUserByUsername(String phone) {
        AppUser userModel = this.UserService.getUserByPhone(phone);
        if (userModel == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return new UserDetailsImpl(userModel);
    }

    public UserDetails loadUserByPhone(String phone) {
        AppUser userModel = this.UserService.getUserByPhone(phone);
        if (userModel == null) {
            return null;
        }
        return new UserDetailsImpl(userModel);
    }
}
